package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.JsonResult;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiredPermission(name = "员工查询")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo result = employeeService.query(qo);
        model.addAttribute("pageInfo", result);
        model.addAttribute("depts", departmentService.list());
        return "employee/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        model.addAttribute("depts", departmentService.list());
        model.addAttribute("roles", roleService.list());
        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
        }
        return "/employee/input";
    }

    @RequiredPermission(name = "员工删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Model model, Long id) {
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            return new JsonResult(false, "删除失败");
        }
        return new JsonResult(true, "删除成功");
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Model model, Long[] ids) {
        System.out.println(ids);
        try {
            employeeService.batchDelete(ids);
        } catch (Exception e) {
            return new JsonResult(false, "删除失败");
        }
        return new JsonResult(true, "删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult save(Employee employee, Long[] ids) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee, ids);
            } else {
                employeeService.save(employee, ids);
            }
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }

        return new JsonResult(true, "操作成功");
    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response)throws Exception {
        // 设置文件下载头
        response.setHeader("Content-disposition", "attachment;filename=text.xls");
        // 创建 Workbook
        Workbook workbook = employeeService.export();
        try {
            // 输出到浏览器
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/import")
    public String inports(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        employeeService.imports(inputStream);
        return "redirect:/employee/list.do";
    }
}
